package com.ruoyi.order.listenrefund;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.ruoyi.order.domain.TbOrder;
import com.ruoyi.order.mapper.TbOrderMapper;
import com.ruoyi.order.utils.SmsCodeUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@RabbitListener(queues = "queue_1")
@Component
public class listenrefundorder {
    @Autowired
    TbOrderMapper tbOrderMapper;
    @Autowired
    RedisTemplate redisTemplate;

    @RabbitHandler
    public void cuserOrderrefund(Channel channel, Message message,Integer orderId){
        String messageId = message.getMessageProperties().getMessageId();
        Object o = redisTemplate.opsForValue().get(messageId);
        if(o!=null){
            try {
                //通过订单id 查询订单
                TbOrder tbOrder = tbOrderMapper.selectTbOrderById(Long.valueOf(orderId));
                //查询总结额
                String totalAmount = tbOrder.getTotalAmount();
                //调用支付 退款接口进行退款
                //退款完成 发送消息给用户
                Map map=new HashMap();
                map.put("code",8888);
                System.out.println("线程二"+Thread.currentThread().getName());
                String jsonString = JSON.toJSONString(map);
                SmsCodeUtils.sendSms("思远","SMS_158845051","18602728510",jsonString);
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else{
            System.out.println("消息被消费");
        }

    }

}
